
//领取新用户新手奖励
const ResCode = require("../config/ResCode");
const Stype = require("../config/Stype");
const Cmd = require("../config/Cmd");
const GameCfg = require('../config/gameCfg');
const log = require("../utils/log");
const redisBase = require("../common/RedisBase");

class  hallNewReward{


    constructor() {
        //新手奖励数据
        this.rewards =  [
            {
                rewardType: GameCfg.RewardType.Type_Quan,
                rewardNum: 100
            },
            {
                rewardType: GameCfg.RewardType.TYpe_QiuNum,
                rewardNum: 888
            }
        ];
    }
    static getInstance(){
        if(!hallNewReward.instance){
            hallNewReward.instance = new hallNewReward();
            return hallNewReward.instance;
        }else{
            return hallNewReward.instance;
        }
    }

    //获取新手奖励数据列表
    getNewRewards(type){
        let ret= {
            type: type,
            data: {
                rewards:this.rewards
            }
        }
        return ret;
    }

    getRewards(){
        return this.rewards;
    }

    getNewRewardRes(_type){
        let ret= {
            type: _type,
            data: {
                code: GameCfg.Respones.OK
            }
        }
        return ret;
    }

    updateUserBroad(data){
        let ret = {
            type: Cmd.Hall.UpdateUserInfoNotice,
            data: {
                userInfo: {
                    userId: data.userId,
                    name: data.name,
                    avator: data.avator,
                    ballNum: data.ballNum*1,
                    ticket: data.ticket*1,
                    ranks: data.ranks*1,
                    score: data.score*1
                }
            }
        }
        return ret;
    }

    //领取新手奖励
    async get_new_player_reward(data,ctype,client){
        let uid = data.userId;
        let ret = this.getRewards();
        let user = await redisBase.getUserInfo(uid);
        //更新user redis
        log.info('=====user===',user);
        let numQiu = 0;
        let numQuan = 0;
        for (let i = 0; i < ret.length; i++) {
            if (ret[i].rewardType === GameCfg.RewardType.Type_Quan) {
                numQuan = user.ticket * 1 + ret[i].rewardNum;
                await redisBase.updateUserQuanNum(uid, numQuan);
                user.ticket = numQuan;
            } else if (ret[i].rewardType === GameCfg.RewardType.TYpe_QiuNum) {
                numQiu = user.ballNum * 1 + ret[i].rewardNum;
                await redisBase.updateUserQiuNum(uid, numQiu);
                user.ballNum = numQiu;
            }
        }
        // 更新user mysql
        await global.hallSerMgr.hallDb.update_user_rewards(uid,numQiu,numQuan,2);

        //更新redis isNewPlayer属性
        redisBase.updateUserisNewPlayer(uid,2);

        //通知用户数据发生变化
        log.info('========get_new_player_reward===========',user);
        let res2 = this.updateUserBroad(user);
        client.send(JSON.stringify(res2));

        //领取奖励返回
        let res = this.getNewRewardRes(ctype);
        return res;
    }
}

module.exports = hallNewReward.getInstance();